package com.taichuan.selfcheck.ad;

import android.media.MediaPlayer;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;


import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by OISNY on 2016/7/20.
 */
public class ADVideoPresenter implements VideoInterface.OnMediaPlayerListener,
        VideoInterface.OnSurfaceViewListener {
    private ADViewPresenter mADViewPresenter;
    private ADArea mADArea;
    private List<ADReleaseView> mVideoLists = new ArrayList<ADReleaseView>();
    private VideoInterface mVideoInterface;
    private ADViewInterface.VideoPresenterInterface mVideoPresenterInterface;
    private VideoInterface.PlayAttrs mCurrentPlayAttrs;

    ADVideoPresenter(ADViewPresenter adViewPresenter) {
        mADViewPresenter = adViewPresenter;
        mADArea = mADViewPresenter.getADArea();
        initVideoInterface();
    }

    private void initVideoInterface() {
        mVideoInterface = new VideoInterface(mADViewPresenter.getContext());
        mVideoInterface.setOnMediaPlayerListener(this);
        mVideoInterface.setOnSurfaceViewListener(this);
    }

    public void setVideoPresenterInterface(ADViewInterface.VideoPresenterInterface videoPresenterInterface) {
        mVideoPresenterInterface = videoPresenterInterface;
    }

    /**
     * 视频目录
     */
    public static String getVideoRootPath() {
        return Environment.getExternalStorageDirectory().getPath() + "/taichuan/video/test/";
    }

    public void loadVideoLists() {
        String videoPath=getVideoRootPath();
        File dirFile = new File(videoPath);
        if (!dirFile.exists()) {
            Log.d(getClass().getSimpleName(),
                    "dirFile dir is not exists , create : "+videoPath);
            boolean createFile=dirFile.mkdirs();
            Log.d(getClass().getSimpleName(),
                    "createFile : "+createFile);
        }
        File videoFile=new File(videoPath,"test.mp4");
        if(videoFile.exists()){
            ADReleaseView adReleaseView=new ADReleaseView();
            adReleaseView.setCachePath(videoPath+"test.mp4");

            synchronized (mVideoLists) {
                mVideoLists.clear();
                mVideoLists.add(adReleaseView);
                log("loadVideoLists VideoLists size : " + mVideoLists.size());
            }
        }
    }

    public void main() {
        synchronized (mVideoLists) {
            log("main init PlayAttrs current VideoLists size : " + mVideoLists.size());
            if (mCurrentPlayAttrs == null) {
                log("main init PlayAttrs null to create  ");
                mCurrentPlayAttrs = new VideoInterface.PlayAttrs(mVideoLists.get(0).getCachePath(),
                        mADArea.isAutoFix());
            } else {
                String playPath = mCurrentPlayAttrs.getPlayPath();
                int position = mCurrentPlayAttrs.getPlayPosition();
                if (position < mVideoLists.size()) {
                    if (playPath.equals(mVideoLists.get(position).getCachePath())) {
                        log("main init PlayAttrs exists to continue : " + mCurrentPlayAttrs.toString());
                        return;
                    }
                }
                log("main init PlayAttrs exists not same to reset 0 : " + mCurrentPlayAttrs.toString());
                mCurrentPlayAttrs = new VideoInterface.PlayAttrs(mVideoLists.get(0).getCachePath(),
                        mADArea.isAutoFix());
            }
        }
    }

    protected boolean isEmpty() {
        boolean isEmpty = false;
        synchronized (mVideoLists) {
            isEmpty = mVideoLists.isEmpty();
        }
        return isEmpty;
    }

    public void play(SurfaceHolder holder) {
        mVideoInterface.play(holder, mCurrentPlayAttrs);
    }

    public void release() {
        mVideoInterface.release();
    }

    @Override
    public void onCompletion(MediaPlayer arg0, VideoInterface.PlayAttrs attrs) {
        playNext();
    }

    @Override
    public void onError(MediaPlayer arg0, VideoInterface.PlayAttrs attrs) {
        playNext();
    }

    private void playNext() {
        synchronized (mVideoLists) {
            int size = mVideoLists.size();
            int currentPosition = mCurrentPlayAttrs.getPlayPosition();
            log("playNext current : " + currentPosition + " ,size : " + size);
            if (currentPosition < size - 1) {
                currentPosition += 1;
                mCurrentPlayAttrs.setPlayPosition(currentPosition);
                mCurrentPlayAttrs.setPlayPath(mVideoLists.get(currentPosition).getCachePath());
                log("playNext current PlayAttrs : " + mCurrentPlayAttrs.toString());
                mVideoInterface.play(mCurrentPlayAttrs);
            } else {
                if (!isEmpty()) {
                    currentPosition = 0;
                    mCurrentPlayAttrs.setPlayPosition(currentPosition);
                    mCurrentPlayAttrs.setPlayPath(mVideoLists.get(currentPosition).getCachePath());
                } else {
                    mCurrentPlayAttrs = null;
                }
                log("playNext to reset ");
                mADViewPresenter.reset(ADArea.PlayedType.IMAGE);
            }
        }
    }

    @Override
    public void onSurfaceViewChanged(int width, int height) {
        if (mADArea.isAutoFix()) {
            log("AutoFix : width : " + width + " , height : " + height);
            mVideoPresenterInterface.onSurfaceViewChanged(width, height);
        }
    }

    private void log(String log) {
        Log.d(getClass().getSimpleName(),
                " #[ VideoSurface ]# " + log);
    }
}
